package com.loongtech.bi.manager.player;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleSchoolLeftManager extends TypeLeftManager<Object> {
    private static final long serialVersionUID = 1L;

    /**
     * 根据开始结束时间和类型及类型，查询所有留存数据
     *
     * @param date
     * @return
     */
    public <T> List<T> getRoleSchoolLeftByDate(Date date, String queryPlatform, String queryChannel, String queryServer, Class<?> c) {
        return findByQuery("from " + c.getSimpleName() + " where 1=1 and day=:date and platformid = '" + queryPlatform + "' and channelid = '" + queryChannel + "' and serverid = '" + queryServer + "' and roleactive is not null and rolecreate is not null order by school ", "date", date);
    }

    public List<Object[]> getDayRoleSchoolLeftByAllKind(Date dateStart, Date dateEnd, Class<?> c, String queryPlatform, String queryChannel, String queryServer) {
        return findByQuery("select date_format(day,'%Y-%m-%d'),roleactive * 1.0 / rolecreate from " + c.getSimpleName() + " where day>=:dayStart and day<=:dayEnd and platformid = '" + queryPlatform + "' and channelid = '" + queryChannel + "' and serverid = '" + queryServer + "' and roleactive is not null and rolecreate is not null group by day order by day desc", "dayStart", dateStart, "dayEnd", dateEnd);
    }

    /**
     * 根据开始结束时间及类型，求留存率
     *
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<Object[]> getRoleSchoolLeftByAllKind(Date dateStart, Date dateEnd, Class<?> c, String queryPlatform, String queryChannel, String queryServer) {
        return findByQuery("select date_format(day,'%Y-%m-%d'),sum(roleactive) * 1.0 /sum(rolecreate) from " + c.getSimpleName() + " where day>=:dayStart and day<=:dayEnd and platformid = '" + queryPlatform + "' and channelid = '" + queryChannel + "' and serverid = '" + queryServer + "' and roleactive is not null and rolecreate is not null group by day order by day desc", "dayStart", dateStart, "dayEnd", dateEnd);
    }

    /**
     * 根据开始结束时间及类型，求留存率
     *
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<Object[]> getRoleSchoolLeftByPlatformGroupByPlatform(Date dateStart, Date dateEnd, Class<?> c) {
        return findByQuery("select platformid,sum(roleactive) * 1.0 /sum(rolecreate) from " + c.getSimpleName() + " where day>=:dayStart and day<=:dayEnd and platformid != '0' and roleactive is not null and rolecreate is not null group by platformid", "dayStart", dateStart, "dayEnd", dateEnd);
    }

    /**
     * 根据开始结束时间及类型，求留存率
     *
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<Object[]> getRoleSchoolLeftByChannelGroupByChannel(Date dateStart, Date dateEnd, Class<?> c) {
        return findByQuery("select channelid,sum(roleactive) * 1.0 /sum(rolecreate) from " + c.getSimpleName() + " where day>=:dayStart and day<=:dayEnd and channelid != '0' and roleactive is not null and rolecreate is not null group by channelid", "dayStart", dateStart, "dayEnd", dateEnd);
    }
}
